package se.svt.utils
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	
	public class LibraryAsset extends EventDispatcher
	{
		private var _isLoaded:Boolean;
		private var _files:Array;
		private var _loader:Loader;
	
		/** Storage for the singleton instance. */
	  	private static const _instance:LibraryAsset = new LibraryAsset( SingletonLock );

	  	public static function get instance():LibraryAsset
	  	{
	   		return _instance;
	   	}
		
		public function getDefinition(name:String):* 
		{ 
			return _loader.contentLoaderInfo.applicationDomain.getDefinition(name);
		}
		
		public function hasDefinition(name:String):Boolean 
		{ 
			return _loader.contentLoaderInfo.applicationDomain.hasDefinition(name);
		}

		public function getSymbolInstance(name:String):* 
		{ 
			try {
				var classReference:Class = _loader.contentLoaderInfo.applicationDomain.getDefinition(name) as Class;
				return new classReference();
			} catch (e:ReferenceError) {
				throw new Error( "[ " +name + " ] Invalid Library Symbol Name." );
			}		
		}


	  	/**
	  	 * Constructor
	  	 * 
	  	 * @param lock The Singleton lock class to prevent outside instantiation.
	  	 */
		public function LibraryAsset( lock:Class )
		{
			// Verify that the lock is the correct class reference.
			if ( lock != SingletonLock )
			{
				throw new Error( "Invalid Singleton access.  Use LibraryAsset.instance." );
			}
			// Normal construction can continue here
			_isLoaded = false;
			_files = [];
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
        	_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
        	_loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
		}

	  	public function get isLoaded():Boolean
	  	{
	   		return _isLoaded;
	   	}

		public function load(swfLibs:Array):void
		{
	        var request:URLRequest = new URLRequest(swfLibs[0]);

			// child SWF adds its unique definitions to
			// parent SWF; both SWFs share the same domain
			// child SWFs definitions do not overwrite parents
			var addedDefinitions:LoaderContext = new LoaderContext();
			addedDefinitions.applicationDomain = ApplicationDomain.currentDomain;

	        _loader.load(request,addedDefinitions);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);

//			vent.INIT,initListner);
//			_loader.load( new URLRequest(files[0]) , new LoaderContext(false,ApplicationDomain.currentDomain) );
		}

		private function completeHandler(event:Event):void {
			_isLoaded = true;
			dispatchEvent(event);
		}

	    private function ioErrorHandler(e:Event):void {
	        dispatchEvent(e);
	    }

	    private function securityErrorHandler(e:Event):void {
	       dispatchEvent(e);
	    }
	}
}

internal class SingletonLock
{
}